package com.lecloud.playerdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lecloud.lecloudsdkdemo2.R;
import com.lecloud.sdk.config.LeCloudPlayerConfig;
import com.lecloud.sdk.http.logutils.LeLog;

import java.io.File;
import java.util.Locale;

public class HomeActivity extends Activity implements OnClickListener {
    private Button vod_hasSkin;
    private Button vod_noSkin;

    private Button live_hasSkin;
    private Button live_noSkin;

    private Button active_player_hasSkin;
    private Button active_player_noSkin;

    private Button mobile_player_hasSkin;
    private Button mobile_player_noSkin;

    private Button other;
    private Button feed_back;
    private Button description;

    private RadioButton mEnBtn;
    private RadioButton mZhBtn;

    private TextView mVersionText;

    String docUrl = "";

    SharedPreferences sp;

    private final int TYPE_ALL = 0;
    private final int TYPE_LIVE = 1;
    private final int TYPE_VOD = 2;
    private final int TYPE_MOBILE = 2;

    private int appType = TYPE_ALL;

    private int hostType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        setOnClickListener();
        sp = getSharedPreferences("host", Context.MODE_PRIVATE);
        hostType = sp.getInt("host", LeCloudPlayerConfig.HOST_DEFAULT);
        switchLanguage(hostType);
        if (hostType == LeCloudPlayerConfig.HOST_US) {
            mEnBtn.setChecked(true);
        } else {
            mZhBtn.setChecked(true);
        }
        setViewVisibility();
    }

    private void initView() {
        vod_hasSkin = (Button) findViewById(R.id.vod_hasSkin);
        vod_noSkin = (Button) findViewById(R.id.vod_noSkin);
        live_hasSkin = (Button) findViewById(R.id.live_hasSkin);
        live_noSkin = (Button) findViewById(R.id.live_noSkin);
        active_player_hasSkin = (Button) findViewById(R.id.active_player_hasSkin);
        active_player_noSkin = (Button) findViewById(R.id.active_player_noSkin);
        mobile_player_hasSkin = (Button) findViewById(R.id.mobile_player_hasSkin);
        mobile_player_noSkin = (Button) findViewById(R.id.mobile_player_noSkin);
        other = (Button) findViewById(R.id.other);
        description = (Button) findViewById(R.id.description);
        mVersionText = (TextView) findViewById(R.id.version_txt);
        mVersionText.setText(getResources().getString(R.string.versionCode) + getVersionName(this));
        feed_back = (Button) findViewById(R.id.clear_log);
        mEnBtn = (RadioButton) findViewById(R.id.en_btn);
        mZhBtn = (RadioButton) findViewById(R.id.zh_btn);
    }

    private void setViewVisibility() {
        if (hostType == LeCloudPlayerConfig.HOST_US) {
            active_player_hasSkin.setVisibility(View.GONE);
            active_player_noSkin.setVisibility(View.GONE);
            mobile_player_hasSkin.setVisibility(View.GONE);
            mobile_player_noSkin.setVisibility(View.GONE);
        }

        if (appType == TYPE_LIVE) {
            vod_hasSkin.setVisibility(View.GONE);
            vod_noSkin.setVisibility(View.GONE);
            mobile_player_hasSkin.setVisibility(View.GONE);
            mobile_player_noSkin.setVisibility(View.GONE);
            other.setVisibility(View.GONE);
        } else if (appType == TYPE_VOD) {
            active_player_hasSkin.setVisibility(View.GONE);
            active_player_noSkin.setVisibility(View.GONE);
            mobile_player_hasSkin.setVisibility(View.GONE);
            mobile_player_noSkin.setVisibility(View.GONE);
        } else if (appType == TYPE_MOBILE) {
            active_player_hasSkin.setVisibility(View.GONE);
            active_player_noSkin.setVisibility(View.GONE);
            vod_hasSkin.setVisibility(View.GONE);
            vod_noSkin.setVisibility(View.GONE);
            other.setVisibility(View.GONE);
        }
    }

    private void setOnClickListener() {
        vod_hasSkin.setOnClickListener(this);
        vod_noSkin.setOnClickListener(this);
        live_hasSkin.setOnClickListener(this);
        live_noSkin.setOnClickListener(this);
        active_player_hasSkin.setOnClickListener(this);
        active_player_noSkin.setOnClickListener(this);
        mobile_player_hasSkin.setOnClickListener(this);
        mobile_player_noSkin.setOnClickListener(this);
        other.setOnClickListener(this);
        feed_back.setOnClickListener(this);
        description.setOnClickListener(this);
        mEnBtn.setOnClickListener(this);
        mZhBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.active_player_hasSkin: {
                jumpActivePlayActivity(true);
                break;
            }
            case R.id.active_player_noSkin: {
                jumpActivePlayActivity(false);
                break;
            }

            case R.id.clear_log: {
                LeLog.clearLog();
                break;
            }
            case R.id.en_btn: {
                switchLanguage(LeCloudPlayerConfig.HOST_US);
                restartActivity();
                break;
            }
            case R.id.zh_btn: {
                switchLanguage(LeCloudPlayerConfig.HOST_DEFAULT);
                restartActivity();
                break;
            }
            default:
                break;
        }
    }



    private void jumpActivePlayActivity(boolean hasSkin) {
        Intent mIntent = new Intent(HomeActivity.this, ActionActivity.class);
        mIntent.putExtra("hasSkin", hasSkin);
        startActivity(mIntent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        exit();
    }

    private void exit() {
//        LeCloudPlayerConfig.destory();
//        System.exit(0);
    }

    /**
     * @param langId
     */
    private void switchLanguage(int langId) {
        LeCloudPlayerConfig.setHostType(langId);
        Editor editor = sp.edit();
        editor.putInt("host", langId);
        editor.commit();

        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        switch (langId) {
            case LeCloudPlayerConfig.HOST_US:
                config.locale = Locale.ENGLISH;
                break;
            default:
                config.locale = Locale.SIMPLIFIED_CHINESE;
                break;
        }
        resources.updateConfiguration(config, dm);


    }

    private void restartActivity() {
        finish();
        Intent intent = new Intent();
        intent.setClass(this, HomeActivity.class);
        startActivity(intent);
    }

    public static String getVersionName(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
            return context.getString(R.string.versionCode);
        }
    }
}
